﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxControlToolkit;
using System.Web.UI.WebControls;
public partial class UserControl_MyIndexControl2 : System.Web.UI.UserControl
{
    private int maDanhMucHienDauTien;

    public int MaDanhMucHienDauTien
    {
        get { return maDanhMucHienDauTien; }
        set { 
            maDanhMucHienDauTien = value; 
         
        }
    }
   
    private string tenDanhMuc;

    public string TenDanhMuc
    {
        get { return tenDanhMuc; }
        set
        {
            tenDanhMuc = value;
            lblTenDanhMuc.Text = "Danh Mục:" + tenDanhMuc;
        }

    }

    public void setTextandLinkDanhMucChinh(string text, string link)
    {
        this.TenDanhMuc = text;
        lblTenDanhMuc.NavigateUrl = link;
    }

    private AdRotator quangCao;

    public AdRotator QuangCao
    {
        get { return quangCao; }
        set { 
            quangCao = value;
           // AdRotator1 = quangCao;
        }
    }

    public void setLinkQuangCao(string link)
    {
        //quangCao.AdvertisementFile = link;
       // AdRotator1.AdvertisementFile = link;
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        
       
    }

    public void addDanhMucCon(int MaDanhMucCon, string TenDanhMucCon)
    {
        Button btn = new Button();
        btn.Text = TenDanhMucCon;
        btn.BackColor = System.Drawing.Color.FromName("#7D7D7D");
        btn.BackColor = System.Drawing.Color.FromArgb(56, 56, 56);
        btn.ForeColor = System.Drawing.Color.WhiteSmoke;
        btn.Font.Bold = true;
        btn.Width = 80;
        btn.Height = 25;
      

       
        btn.ID = MaDanhMucCon.ToString();

        btn.Click += new EventHandler(btn_Click);
     
        pnlButton.Controls.Add(btn);
       
        

    }

    public void btn_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;

       
        for (int i = 0; i < pnlButton.Controls.Count; i++ )
        {
            if(pnlButton.Controls[i] is Button)
            {
                Button btn1 = (Button)pnlButton.Controls[i];
                btn1.BackColor = System.Drawing.Color.FromArgb(56, 56, 56);
            }
          
        }
        btn.BackColor = System.Drawing.Color.FromArgb(96, 96, 96);
        int ma = int.Parse(btn.ID);
        
        getData(ma);
    //    btn.BackColor = Color.Blue;


    }

    public void getData( int ma)
    {
        DateTime now = DateTime.Now;
        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();
        var query = from tinraovat in linQ.TIN_RAO_VATs
                    where tinraovat.MaDanhMucCon == ma && tinraovat.MaLoaiTinRaoVat == CMyStaticFunction.layMaTinRaoVatHot()
                    && tinraovat.Deleted == false && tinraovat.ViPham== false && tinraovat.ThoiGianDang <= now
                    select tinraovat;
        gridVIP.DataSource = query.OrderByDescending(p => p.ThoiGianDang).Take(5);
        
        gridVIP.DataBind();

        query = from tinraovat in linQ.TIN_RAO_VATs
                where tinraovat.MaDanhMucCon == ma && tinraovat.MaLoaiTinRaoVat == CMyStaticFunction.layMaTinRaoVatThuong()
                && tinraovat.Deleted == false && tinraovat.ViPham == false && tinraovat.ThoiGianDang <= now
                select tinraovat;
       
       gridNormal.DataSource = query.OrderByDescending(p => p.ThoiGianDang).Take(5);
        gridNormal.DataBind();
    }

    protected void gridNormal_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gridVIP_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TIN_RAO_VAT objTin = (TIN_RAO_VAT)e.Row.DataItem;
            System.Web.UI.WebControls.Image img = (Image)e.Row.FindControl("Image1");

            if (objTin.MaLoaiTinRaoVat == CMyStaticFunction.layMaTinRaoVatHot())
            {

                img.ImageUrl = "~/Images/hot.gif";
            }
            else
            {
                img.Visible = false;
            }

            Image imgSanPham = (Image)e.Row.FindControl("Image2");
            if (objTin.Thumbnail == null)
            {
                HyperLink hp = (HyperLink)e.Row.FindControl("HyperLink1");
                hp.NavigateUrl = @"~/Images/NotImage.jpg";
                imgSanPham.ImageUrl = @"~/Images/NotImage.jpg";
            }
            else
            {
                HyperLink hp = (HyperLink)e.Row.FindControl("HyperLink1");

                hp.NavigateUrl = @"~/RaoVat/ShowImage.aspx?id=" + objTin.MaTinRaoVat;
                // hp.NavigateUrl = @"~/Images/NotImage.jpg";
                imgSanPham.ImageUrl = @"~/RaoVat/ShowImage.aspx?id=" + objTin.MaTinRaoVat;

            }

        }
    }
    protected void gridNormal_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
}



